var express = require('express');
var router = express.Router();
var multiparty=require('multiparty')
var jwt=require('jsonwebtoken')
var { UsersModel, BooksModel, CategoriesModel, FavoritesModel,userModel } = require('../model/model');

router.post('/login', async (req, res) => {
  console.log('====================================');
  console.log(req.body);
  console.log('====================================');
    let { username, password } = req.body
    let token=jwt.sign({username:"huxubo"},"123456",{expiresIn:'265d'})
    let finduser = await userModel.find({ username })
    console.log('====================================');
    console.log(finduser.length);
    console.log('====================================');
    if (finduser.length > 0) {
      if (finduser[0].password == password) {
          res.send({
          code: 200,
          token
        })
      } else {
         res.send({
          code: 201,
          msg: '密码不正确'
        })
      }
    } else {
       res.send({
        code: 201,
        msg: '用户名不存在'
      })
    }
  })
  

  router.get('/code', (req, res) => {
    //引入验证码中间件
    let svgCaptha = require('svg-captcha')
    // let capthca=svgCaptha.create();//创建   实例化
    let capthca = svgCaptha.create({
      noise: 3, //干扰线条数
      size: 4,//验证码字符个数
      color: true,     //字符颜色
      background: "gray",    //背景色
      ignoreChars: "0oiIz21"  //忽略的字符
    });
  
    console.log(capthca.text);//生成文字版
    let qwe=capthca.text
    res.type("svg");//设置头信息  指定响应资源为图片类型
    //生成图片版  catpcha.data
    // console.log('====================================');
    // console.log(svg);
    // console.log('====================================');
    res.status(200)
    let asd=capthca.data
    res.send({asd,qwe})
  })




module.exports = router;
